insheet using ../Data/Raw/raw_recruiter_data.csv, comma names clear

drop instance candidatecollegejobid candidateinterntitleid candidatesecondjobtitleid candidatejobtitleid candidateinternid candidatesecondjobtextid candidatejobtextid candidateschooltextid candidatesecondjobcompany candidateskillid jobcompanyassigner  schoolassigner lastnameassigner firstnameassigner candidateurl candidatestreetaddress candidatezipcode candidateareacode candidatephoneprefix candidatephonesuffix candidatefirstname candidatelastname candidateschool candidateschoollocations candidatejobcompany candidatecompanylocations candidateskilltext candidatesecondcompanylocation candidateschooltext candidatesecondjobtext candidatejobtext candidateinternjoblocation  candidateinterntitle candidatecollegejobtitle candidatecollegejobcompany candidatehascollegejob  candidateschoolgradyear candidateinternyear candidatecollegejobyear candidatesecondjobstartyear candidatesecondjobendyear candidatejobstartyear candidatejobendyear candidatesecondjobstartdate candidatesecondjoblength candidatepreviousjobstartdate hiringmanagerfirstname hiringmanagerlastname hiringmanagerschool hiringmanagerjobcompany hiringmanagerschoollocations

gen hmw=hiringmanageriswhite
gen hmm=hiringmanagerismale
gen hme=hiringmanagereliteschool
gen cw=candidateiswhite
gen cm=candidateismale
gen ce=candidateeliteschool
gen cb=candidatebigcompanyexp
gen current_softwaretitle=strpos(candidatejobtitle, "Software")
gen prev_softwaretitle=strpos(candidatesecondjobtitle, "Software")
gen ever_softwaretitle = current_softwaretitle | prev_softwaretitle
gen ccst=ever_softwaretitle
drop current_softwaretitle prev_softwaretitle ever_softwaretitle
gen candidate_age=18+2017-candidateschoolstartyear
gen phire=pacceptinterview*ppassinterview*pacceptjoboffer
gen screenerloghourlyrateamount=log(screenerhourlyrateamount)



label variable cw "White Candidate"
label variable ce "Elite School Candidate"
label variable cm "Male Candidate"
label variable cb "Big Company Candidate"
label variable ccst "Candidate w/ ``Software Engineering'' Title"

label variable pacceptinterview "P(Accept Interview)"
label variable ppassinterview "P(Pass Interview)"
label variable pacceptjoboffer "P(Accept Job Offer)"
label variable interview "Interviewed" 



gen gave_explanation=!missing(comments)
la var gave_explanation "Gave explanation"


gen pworkeraccept=(pacceptjoboffer+pacceptinterview)/2
la var pworkeraccept "Average P(Accept)"




gen has_comment=!missing(comment)
gen screenerisfemale=screenerismale==0
gen priorscreeningexperience=1

gen totalhours2=totalhours 
replace totalhours2=0 if mi(totalhours2)


label variable screenerisfemale "Female"
label variable screeneriswhite "White"
label variable screenerisblack "Black"
label variable priorscreeningexperience "Prior Recruiting Experience"
label variable screenerhourlyrateamount "Hourly Rate"
label variable totalhours2 "Total Hours Spent on 16 Resumes"
label variable phire "P(Hired)"
label variable has_comment "Explained Choice"




egen ppassinterview_std=std(ppassinterview)
egen pacceptinterview_std=std(pacceptinterview)
egen pacceptjoboffer_std=std(pacceptjoboffer)


* Merge in additional covars


merge m:1 screenerid using ../Data/Raw/extra-covars-merge.dta, nogen


* Additional cleaning


* Cleaning 
la var screenerisfemale "Female screener"
gen pinterviewoffer =  pacceptinterview  *pacceptjoboffer 
la var pinterviewoffer "P(Accepts interview and offer)"

* Switch base group categories
gen cf = !cm
la var cf "Female Job Applicant"
gen cbl = !cw
la var cbl "Black Job Applicant"

gen hmf = !hmm & !hiringmanagerisempty
la var hmf "Female Hiring Manager"
gen hmbl = !hmw & !hiringmanagerisempty
la var hmbl "Black Hiring Manager"






* Labels
label variable pacceptinterview "P(Accepts Interview | Interview Extended)"
label variable ppassinterview "P(Passes Interview | Interviewed)"
label variable pacceptjoboffer "P(Accepts Offer | Offer Extended)"
label variable pworkeraccept "Avg P(Applicants Accept)"
label variable pworkeraccept "Avg P(Applicants Accept)"

label variable hmm "Male Hiring Manager"
label variable hmw "White Hiring Manager"
label variable hme "Elite School Hiring Manager"
label variable cm "Male Job Applicant"
label variable cw "White Job Applicant"
label variable ce "Elite School Job Applicant"
label variable cb "Big Company Job Applicant"
label variable candidate_age "Candidate Age (Years)"
label variable hiringmanagerisempty "Blinded Hiring Manager"
label variable pacceptinterview "P(Accepts Interview)"
label variable ppassinterview "P(Passes Interview)"
label variable pacceptjoboffer "P(Accepts Offer)"
label variable pworkeraccept "Avg P(Applicants Accept)"

la var screeneriswhite "White screener"
la var screenerisblack "Black screener"
la var screenerhourlyrateamount "Hourly rate"
la var totalhours2 "Hours on task"
la var hiringmanagerismale "Male hiring manager"
la var hiringmanagereliteschool "Elite school hiring manager"
la var hiringmanageriswhite  "White hiring manager"
la var hiringmanagerisempty "Empty hiring manager"
la var candidateiswhite  "White candidate"
la var candidateismale  "Male candidate"
la var candidateeliteschool "Elite school candidate"
la var candidatebigcompanyexp "Big company candidate"


xtile phire_decile= phire, nq(10)


* Convert probabilities into rank order
sort screenerid
by screenerid: egen p1_rank=rank(pacceptinterview), field
by screenerid: egen p2_rank=rank(ppassinterview), field
by screenerid: egen p3_rank=rank(pacceptjoboffer), field
by screenerid: egen phire_rank=rank(phire), field

la var p1_rank "Rank of P(Accept Interview)"
la var p2_rank "Rank of P(Pass Interview)"
la var p3_rank "Rank of P(Accept Offer)"
la var phire_rank "Rank of P(Hire)"


sort screenerid
by screenerid: gen randomfirstcandidate=_n==1
la var randomfirstcandidate "Random first candidate"

* create match type indicators
egen match_type=group(hmw hmm hme hiringmanagerisempty cw cm ce cb), label

* Homophilhy
gen bothmale =  cm==1 & hmm==1
la var bothmale "Both male"
gen bothfemale =  cm==0 & hmm==0
la var bothfemale "Both female"
gen bothwhite = cw==1 & hmw==1
la var bothwhite "Both white"
gen bothblack = cw==0 & hmw==0
la var bothblack "Both black"
gen bothelite = ce==1 & hme==1
la var bothelite "Both elite school"
gen bothnonelite = ce==0 & hme==0
la var bothnonelite "Both non-elite school"

 
gen gender_hom=hmm==cm
gen race_hom=hmw==cw
gen elite_hom=hme==ce
gen hom_index=gender_hom+race_hom+elite_hom
la var gender_hom "Same gender"
la var race_hom "Same race"
la var elite_hom "Same education status"
la var hom_index "Homophily index, 0--4"

label variable avgfeedbackscore "Avg feedback score"
label variable nofeedback "No feedback"
label variable norevenue "No revenue"
label variable ciphertext "Cipher text"






save ../Data/Clean/cleaned_recruiter_data, replace
